//
//  WBMainController.swift
//  XLWebo
//
//  Created by 陈晓龙 on 17/2/15.
//  Copyright © 2017年 陈晓龙. All rights reserved.
//

import UIKit

class WBMainController: UITabBarController {
 lazy var plusButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()
         delegate = self
        // Do any additional setup after loading the view.
        createChildControllers()
    
         setupPlusButton()
    }

 
    //portrait 竖屏
    //landscape 横屏
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
// END
}





// MARK: - UITabBarControllerDelegate 监听tabbar click
extension WBMainController:UITabBarControllerDelegate{
   
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        
        print("sfsf")
        
        return true
    }
}


// MARK: - extension 类似与oc的分类 实现一些方法
extension WBMainController{
    //中间按钮
    func setupPlusButton() {
        tabBar.addSubview(plusButton)
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width/count
        //设置缩进.正数向内缩进，负数向外扩展
        plusButton.frame = tabBar.bounds.insetBy(dx: 2*w, dy: 0)
        
        plusButton.addTarget(self, action:#selector(self.plusStatus), for: .touchUpInside)
    }
    
//    点击中间按钮
//    private 保证方法私有,仅在当前对象被访问
    //@objc 通过OC的消息机制被访问
    
    @objc private func plusStatus()  {
        print("点击")
        let vc  = UIViewController()
        vc.view.backgroundColor = UIColor .cz_random();
        let nav  = UINavigationController(rootViewController: vc)
        present(nav, animated: true, completion: nil)
        
        
    }
    
    
       func createChildControllers() {
        //获取沙和路径
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as String).appending("/demo.json")
        //转换成data
        var data = NSData(contentsOfFile: jsonPath)
        //判断沙盒是否有data
        if data == nil {
            //如果没有加载本地data
            let path = Bundle.main.path(forResource: "demo.json", ofType: nil)
             data = NSData(contentsOfFile: path!)
            print("加载本地data")
        }
        //加载json文件 初始化初始数据
      guard let array = try? JSONSerialization.jsonObject(with: data as! Data, options: []) as! [[String:AnyObject]] else {
            return
        }
  //循环加载控制器
        var arrayVC = [UIViewController]()
        for dict in array {
       arrayVC.append(controllers(dict: dict as [String : AnyObject]))
        }
     viewControllers = arrayVC
    }
    
    /// - Parameter dict: dict[clsName,title,imageName]
    /// - Returns: 返回
    func controllers(dict:[String:AnyObject]) -> UIViewController {
        guard let clsName = dict["clsName"] as? String,
              let title = dict["title"] as? String,
              let imageName = dict["imageName"] as? String,
              let cls = NSClassFromString(Bundle.main.nameSpace() + "." + clsName) as? BaseViewController.Type,
              let visitorDict = dict["visitorInfo"] as? [String:String]
            else {
        return UIViewController()
        }
       
        let childVC = cls.init()
        childVC.title = title;
        //每个控制器初始化一个访客信息的数据字典
        childVC.visitorInfoDict = visitorDict
        //设置图片
       childVC.tabBarItem.image = UIImage(named:"tabbar_"+imageName)
        childVC.tabBarItem.selectedImage = UIImage(named: "tabbar_"+imageName+"_selected")?.withRenderingMode(.alwaysOriginal)
        //设置title 属性
        childVC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange,NSFontAttributeName:UIFont.systemFont(ofSize: 13)], for: .highlighted)
        childVC.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 13)], for: .normal)
        let nav = WBNavgationController(rootViewController: childVC)
        return nav
        
        
    }
    
}
    
